package com.onedec.attendance.controller;

import com.onedec.attendance.entity.OnedecCheckingIn;
import com.onedec.attendance.service.CheckinService;
import com.onedec.attendance.utils.Page;
import com.onedec.attendance.utils.Result;
import com.onedec.deptfacade.DeptApi;
import org.springframework.beans.factory.annotation.Autowired;


import com.onedec.utils.*;
import org.springframework.web.bind.annotation.*;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.onedec.attendance.utils.DateUtils;

@RestController
public class CheckInController {
    @Autowired
    private CheckinService checkinService;
    @Autowired
    private DeptApi deptApi;

    @RequestMapping("/getdepts")
    public ResponseEntity getDepts(){
        System.out.println("进入deptApi"+deptApi);
        ResponseEntity success = ResponseEntity.SUCCESS;
        success.setData(deptApi.getDepts());
        return success;
    }

    @GetMapping("/getadmins")
    public ResponseEntity getAdmins(){
        System.out.println("进入方法");
        ResponseEntity success = ResponseEntity.SUCCESS;
        success.setData(deptApi.getAdmins());
        return success;
    }

    @RequestMapping("/getAllCheckIn")
    public ResponseEntity getAllCheckIn(){
        System.out.println("进入方法");
        try {
            List<OnedecCheckingIn> list = checkinService.selectAll();
            ResponseEntity success = ResponseEntity.SUCCESS;
            success.setData(list);
            return success;
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ERROR;
        }
    }

    @PostMapping("/createNewCheckIn")
    @ResponseBody
    public ResponseEntity createNewCheckIn(@RequestParam OnedecCheckingIn checkin, List<Integer> empList){
        System.out.println("新的考勤群组："+checkin);
        System.out.println("员工id包括"+empList);
        return ResponseEntity.SUCCESS;
    }

    @GetMapping("/getCheckIn")
    public Result getCheckIn(Page page, Integer did) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        System.out.println("参数为：" + did);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        df.setLenient(false);
//        map.put("start", start);
//        System.out.println("start:" + start);
//        map.put("end", end);
//        System.out.println("end:" + end);
        map.put("pageIndex", page.getPageIndex());
        System.out.println("i" + page.getPageIndex());
        map.put("number", page.getNumber());
        map.put("did", did);
        int count = checkinService.selectCheckINCount();
        page.setDataCount(count);
        page.setPageCount((count - 1) / page.getNumber() + 1);
        List<Map<String, Object>> list = checkinService.selectAllCheckin(map);
//        // 修改时间格式
//        for (Map<String, Object> exam : list) {
//            System.out.println(exam.get("in_morning_closing_time"));
//            java.util.Date timeDate = df.parse(exam.get("in_morning_closing_time") + "");
//            java.sql.Date dateTime = new java.sql.Date(timeDate.getTime());
//            exam.put("in_morning_closing_time", df.format(dateTime));
//            if (exam.get("in_afternoon_hours") != null) {
//                java.util.Date timeDate2 = df.parse(exam.get("in_afternoon_hours") + "");
//                java.sql.Date dateTime2 = new java.sql.Date(timeDate2.getTime());
//                exam.put("in_afternoon_hours", df.format(dateTime2));
//            }
//        }
        System.out.println(list);
        return Result.success(page,list);
    }

    @PostMapping("/deleteCheckin")
    public Result deleteCheckIn(Integer Id){
        System.out.println("进入删除方法："+Id);
        try {
            Integer checkIn = checkinService.deleteCheckIn(Id);
            System.out.println(checkIn);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("哎呀！出错了！");
        }
    }

    @PostMapping("/addAttendance")
    public Result addAttendance(String inCheckingName, String inMorningClosingTime,String inAfternoonClosingTime, Integer inQunzhuId, Integer deptId){
        if(!"".equals(inCheckingName)&&inCheckingName!=null){
            OnedecCheckingIn checkingIn = new OnedecCheckingIn();
            checkingIn.setInCheckingName(inCheckingName);
            try {
                Date morning = DateUtils.HOUR_FORMAT.parse(inMorningClosingTime);
                Date afternoon = DateUtils.HOUR_FORMAT.parse(inAfternoonClosingTime);
                checkingIn.setInQunzhuId(inQunzhuId);
                checkingIn.setInMorningClosingTime(morning);
                checkingIn.setInAfternoonClosingTime(afternoon);
                checkingIn.setInMorningHours(DateUtils.getMorningStart());
                checkingIn.setInAfternoonHours(DateUtils.getAfternoonStart());
                int i = checkinService.insertNewCheckin(checkingIn);
                return Result.success(i,null);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.fail("添加出错");
            }
        }
        return Result.fail("添加出错");
    }
}
